package com.project.study.farming.common.entity.dto;

import lombok.Data;

import java.util.List;

/**
 * 分页查询
 * @param <T> 数据
 * @since 2020-05-02
 */
@Data
public class Page<T> {

    private Integer pageNum = 1;

    private Integer pageSize = 6;

    private Integer size;

    private Integer maxPage;

    private List<T> data;

    public Page(List<T> data){
        this(1, data);
    }

    public Page(Integer pageNum, List<T> data){
        this.pageNum = pageNum;
        this.data = this.limitPages(data, pageNum, pageSize);
        this.size = data.size();
        this.maxPage = this.size == 0 ? 1 : this.size / pageSize + (size % pageSize == 0 ? 0 : 1);
    }

    private List<T> limitPages(List<T> list, int pageNum, int pageSize){
        int start = (pageNum - 1) * pageSize;
        int end = pageNum * pageSize;
        int total = list.size();
        end = Math.min(end, total);
        return list.subList(start, end);
    }

    @SuppressWarnings("all")
    public Integer jump(Integer i){
        int currnt = pageNum + i;
        if(currnt < 1){
            currnt = 1;
        }else{
            currnt = Math.min(currnt, this.maxPage);
        }
        return currnt;
    }
}
